using CSV
using DataFrames
using FFTW
using Plots
using Statistics
using XLSX

# 读取 Excel 文件中的数据
filename = "Active_filtered_data.csv"  # 替换为您的文件名
data = DataFrame(CSV.File(filename))

# 提取各列数据
point_numbers = data[:, 1]
timestamps = data[:, 2]
temperatures = data[:, 3]
measurements = data[:, 4]
active_filter_data = data[:,5]
average_data = mean(measurements)

fft_data = active_filter_data

# t = 1:0.1:500
# y = sin.(t)+sin.(2 .*t)
# y = sin.(2*pi .* t)
# measurements = y
# 设置参数
Ts = 0.02
fft_result = fft(fft_data.-average_data)
N = length(fft_result)
frequencies = (0:N-1) / (N * Ts)  # 频率向量

# 只取一半的频率范围（Nyquist 频率）
half_N = div(N, 2)
fft_magnitude = abs.(fft_result[1:half_N]) ./ N
frequencies_half = frequencies[1:half_N]

# 绘制频谱图
plot(frequencies_half, fft_magnitude, legend=false)
xlims!(0,4)
ylims!(0,0.0001)
xlabel!("frequency(Hz)")
ylabel!("amplitude")
title!("FFT Spectrum")
savefig("FFT_Result.png")

# 显示图表
println("FFT_Result.png saved")